package com.github.natholdallas.controller.user

import com.github.natholdallas.common.OrderType
import com.github.natholdallas.common.USER
import com.github.natholdallas.common.byId
import com.github.natholdallas.common.getBean
import com.github.natholdallas.database.*
import com.github.natholdallas.declaration.Getter
import com.github.natholdallas.declaration.Setter
import java.math.BigDecimal

data class UserIn(
    val username: String,
    val password: String,
    val name: String?,
    val avatar: String?
) : Setter<User> {
    override fun set(v: User) {
        v.username = username
        v.password = password
        v.name = name
        v.avatar = avatar
    }
}


data class StarIn(
    var copilot: Long
) : Getter<StarTable> {
    override fun get() = StarTable(
        copilot = getBean(Users::class).byId(copilot),
        user = USER
    )
}


data class CommentIn(
    var copilot: Long,
    var content: String
) : Getter<CommentTable> {
    override fun get() = CommentTable(
        copilot = getBean(Users::class).byId(copilot),
        user = USER,
        content = content
    )
}


data class TopupIn(
    val description: String?,
    val amount: BigDecimal
) : Getter<TopupTable> {
    override fun get() = TopupTable(
        user = USER,
        description = description,
        amount = amount,
        payment = true
    ).apply {
        user.amount += amount
        user.balance += amount
        amountRecord = user.amount
        balanceRecord = user.balance
    }
}


data class OrderIn(
    val copilot: Long,
    val description: String?,
    val type: OrderType,
    val price: Int,
    val duration: Int,
) : Getter<OrderTable> {
    override fun get() = OrderTable(
        user = USER,
        copilot = getBean(Users::class).byId(copilot),
        description = description,
        type = type,
        price = price,
        duration = duration,
        payment = true
    ).apply {
        totalPrice = price * duration
        iPrice = BigDecimal(totalPrice) * BigDecimal("0.8")
        cPrice = BigDecimal(totalPrice) * BigDecimal("0.2")
        user.balance -= BigDecimal(totalPrice)
        amountRecord = user.amount
        balanceRecord = user.balance
    }
}
